package io.github.weiranyi;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-20 19:48
 **/
public class Sender {
    public static void main(String[] args) {
        // 获取模板对象（从容器中获取）
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbitmq-producer-dlx.xml");
        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
        // 发消息
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("name", "蔚然");
        // 【消息设置过期时间】
        MessageProperties messageProperties = new MessageProperties(); //创建消息配置对象
        messageProperties.setExpiration("19000"); // 设置过期时间9s
        Message message = new Message("消息过期时间".getBytes(),messageProperties); //创建消息
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("dlx_max", hashMap);
            System.out.println("消息已经发出");
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        context.close();
    }
}
